package cn.sdhuijin.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sdhuijin.dao.TargetDao;
import cn.sdhuijin.model.Target;
import cn.sdhuijin.model.TargetScore;
import cn.sdhuijin.utils.CommonUtil;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年5月7日 下午4:07:07 类说明
 */
@Service
@Transactional
public class TargetServiceImpl implements TargetService {

	@Inject
	private TargetDao targetDao;

	@Inject
	private EntityManagerFactory entityManagerFactory;

	@Override
	public void BatchImport(List<List<Object>> llo) throws Exception {
		if (llo != null) {
			for (int i = 1; i < llo.size(); i++) {
				String objects = llo.get(i).get(1).toString().trim();
				if (CommonUtil.allIsNotEmpty(objects)) {
					String[] items = objects.split("/");
					Target parentTarget = null;
					for (int j = 0; j < items.length; j++) {
						Target target = new Target();
						List<Target> existedTarget = null;
						if (j == 0) {
							existedTarget = targetDao.findByName(items[j]);
							if (existedTarget.size() == 0) {
								target.setName(items[j]);
								parentTarget = targetDao.save(target);
							} else {
								parentTarget = existedTarget.get(0);
							}
						} else {
							existedTarget = targetDao.findByNameAndParentId(
									items[j], parentTarget.getId());
							if (existedTarget.size() == 0) {
								target.setName(items[j]);
								target.setParent(parentTarget);
								parentTarget = targetDao.save(target);
							} else {
								parentTarget = existedTarget.get(0);
							}
						}
					}
				}

			}
		}
	}

	@Override
	public List<TargetScore> getTargetScoreDesc() {
		EntityManager entityManager = null;
		Query query = null;
		List<TargetScore> targetScoreList = new ArrayList<TargetScore>();
		try {
			entityManager = entityManagerFactory.createEntityManager();
			query = entityManager
					.createNativeQuery("SELECT t.`name` AS `name`, "
							+ "IFNULL(Sum(d.score),0) AS sumscore "
							+ "FROM target AS t "
							+ "LEFT JOIN deduction AS d ON t.id = d.target_id "
							+ "LEFT JOIN standarditem AS si ON si.id = d.standardItem_id "
							+ "LEFT JOIN standardfolder AS sf ON si.standardFolder_id = sf.id "
							+ "WHERE t.`level` = 1 AND t.isDeleted = 0 AND "
							+ "(d.isDeleted = 0 OR d.isDeleted IS NULL) AND "
							+ "(sf.safeManager = 1 OR sf.safeManager IS NULL )"
							+ "GROUP BY t.id ORDER BY sumscore DESC");
			List list = query.getResultList();
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					TargetScore targetScore = new TargetScore();
					Object[] objs = (Object[]) list.get(i);
					targetScore.setNum(i + 1);
					targetScore
							.setScore(Double.parseDouble(objs[1].toString()));
					targetScore.setName(objs[0].toString());
					targetScore.setUpOrDown(CommonUtil.GenerateRandomNum(0, 2));
					targetScore.setdValue(CommonUtil.GenerateRandomNum(0, 6));
					targetScoreList.add(targetScore);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (entityManager != null) {
				entityManager.close();
			}
		}
		return targetScoreList;
	}

	@Override
	public Target isExist(Long id) {
		Target target = targetDao.findOne(id);
		return target;
	}

}
